/**
 * OSAmI: Open Source Ambient Intelligence
 * Copyright (C) 2010 Bull S.A.S.
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 */

package org.osami.gateway.sensor.internal;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.logging.Logger;

import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.ServiceProperty;
import org.apache.felix.ipojo.annotations.Validate;
import org.osami.gateway.sensor.protocol.Message;
import org.osami.gateway.sensor.protocol.IMessageSender;

/**
 * Created by IntelliJ IDEA.
 * User: guillaume
 * Date: 26 mai 2010
 * Time: 15:45:08
 * To change this template use File | Settings | File Templates.
 */
@Component
@Provides
public class SensorMessageSender implements IMessageSender {
    private static final Logger logger = Logger.getLogger(SensorMessageSender.class.getName());

    @ServiceProperty
    private String hostname;

    @ServiceProperty
    private int port;

    @ServiceProperty
    private String type;

    private DatagramSocket socket;
    private InetAddress address;
    private static final int MAX_SIZE = 1402; // ??
    private boolean do_exit;

    public SensorMessageSender() {

    }

    @Validate
    public void start() throws Exception {
        String actualHostname;
        actualHostname = System.getenv("OSAMI_SENSOR_HOSTNAME");
        if (actualHostname == null) {
            actualHostname = hostname;
        }
        address = InetAddress.getByName(actualHostname);
        logger.info("InetAddress " + actualHostname + " -> " + address);
        socket = new DatagramSocket(port, address);

        logger.info("Bound InetAddress " + socket.getLocalAddress());
    }

    @Invalidate
    public void stop() {
        socket.close();
    }

    public void send(Message message) {
        byte[] buffer = new byte[MAX_SIZE];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length, message.getToAddress(), message.getToPortNumber());
        packet.setData(message.toString().getBytes());

        try {
            socket.send(packet);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
